package co.fitstart.admin.web.controller.market;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Page;
import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.resolver.Preloading;
import com.demo2do.core.web.utils.WebUtils;

import co.fitstart.admin.service.DataService;
import co.fitstart.admin.service.MarketCoursewareService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.entity.market.MarketCourse;
import co.fitstart.entity.market.MarketWechatImage;
import co.fitstart.entity.market.template.MarketMonthlyCoursewareTemplate;

/**
 * 
 * @author dan
 *
 */
@Controller
@RequestMapping("/market-courses")
@MenuSetting("menu-market")
public class MarketCourseController {
    
    @Autowired
    private MarketCoursewareService marketCoursewareService;
    
    @Autowired
    @Qualifier("dataService")
    private DataService<MarketWechatImage> marketWechatImageService;
    
    private static final Logger logger = LoggerFactory.getLogger(MarketCourseController.class);
    
    /**
     * 
     * @param page
     * @param request
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-market-course")
    public ModelAndView list(Page page, HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("market/market-course/market-course-list");
        
        List<MarketCourse> marketCourses = marketCoursewareService.listMarketCourse(page);
        modelAndView.addObject("marketCourses", marketCourses);
        
        modelAndView.addObject("page", page);
        
        modelAndView.addObject("refer", "list");
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
        
        return modelAndView;
    }
    
    /**
     * 
     * @param courseId
     * @return
     */
    @RequestMapping(value = "/{courseId}", method = RequestMethod.GET)
    @MenuSetting("submenu-market-course")
    public ModelAndView view(@PathVariable("courseId") Long courseId) {
        ModelAndView modelAndView = new ModelAndView("market/market-course/market-course-view");
        
        MarketCourse marketCourse = marketCoursewareService.loadMarketCourse(courseId);
        modelAndView.addObject("marketCourse", marketCourse);
        
        return modelAndView;
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public ModelAndView create() {
        ModelAndView modelAndView = new ModelAndView("market/market-course/market-course-create");
        
        return modelAndView;
    }
    
    /**
     * 
     * @param marketCourse
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public @ResponseBody String onCreate(MarketCourse marketCourse) {
        Result result = new Result();
        
        try {
            
            marketCoursewareService.createMarketCourse(marketCourse);
            
            result.success().message("创建课程成功");
            
        } catch (Exception e) {
            
            logger.error("onCreate - ", e);
            result.fail().message("创建课程失败 ");
            
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param courseId
     * @return
     */
    @RequestMapping(value = "/{courseId}/edit", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("courseId") Long courseId) {
        ModelAndView modelAndView = new ModelAndView("market/market-course/market-course-edit");
        
        MarketCourse marketCourse = marketCoursewareService.loadMarketCourse(courseId);
        modelAndView.addObject("marketCourse", marketCourse);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param marketCourse
     * @return
     */
    @RequestMapping(value = "/{courseId}", method = RequestMethod.PATCH)
    public @ResponseBody String onEdit(@Preloading("courseId") MarketCourse marketCourse) {
        Result result = new Result();
        
        try {
            
            marketCoursewareService.editMarketCourse(marketCourse);
            
            result.success().message("修改课程成功");
            
        } catch (Exception e) {
            
            logger.error("onEdit - ", e);
            result.fail().message("修改课程失败 ");
            
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param courseId
     * @return
     */
    @RequestMapping(value = "/{courseId}/cover", method = RequestMethod.GET)
    public ModelAndView editCover(@PathVariable("courseId") Long courseId) {
        ModelAndView modelAndView = new ModelAndView("market/market-course/market-course-cover-edit");
        
        MarketCourse marketCourse = marketCoursewareService.loadMarketCourse(courseId);
        modelAndView.addObject("marketCourse", marketCourse);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param marketCourse
     * @return
     */
    @RequestMapping(value = "/{courseId}/cover", method = RequestMethod.PATCH)
    public @ResponseBody String onEditCover(@Preloading("courseId") MarketCourse marketCourse) {
        Result result = new Result();
        
        try {
            
            marketCoursewareService.editMarketCourse(marketCourse);
            
            result.success().message("修改课程封面成功");
            
        } catch (Exception e) {
            
            logger.error("onEditCover - ", e);
            result.fail().message("修改课程封面失败 ");
            
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param courseId
     * @return
     */
    @RequestMapping(value = "/{courseId}/qrcode", method = RequestMethod.GET)
    public ModelAndView editQrcode(@PathVariable("courseId") Long courseId) {
        ModelAndView modelAndView = new ModelAndView("market/market-course/market-course-qrcode-edit");
        
        MarketCourse marketCourse = marketCoursewareService.loadMarketCourse(courseId);
        modelAndView.addObject("marketCourse", marketCourse);
        
        MarketWechatImage wechatImage = marketCoursewareService.getMarketWechatImage(marketCourse);
        modelAndView.addObject("wechatImage", wechatImage);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param courseId
     * @param qrcode
     * @return
     */
    @RequestMapping(value = "/{courseId}/qrcode", method = RequestMethod.PATCH)
    public @ResponseBody String onEditQrcode(@PathVariable("courseId") Long courseId, @RequestParam("cover") String qrcode) {
        Result result = new Result();
        
        try {
            
            MarketCourse marketCourse = marketCoursewareService.loadMarketCourse(courseId);
            MarketWechatImage wechatImage = marketCoursewareService.getMarketWechatImage(marketCourse);
            
            marketWechatImageService.update(wechatImage.edit(qrcode));
            
            result.success().message("修改群二维码成功");
            
        } catch (Exception e) {
            
            logger.error("onEditQrcode - ", e);
            result.fail().message("修改群二维码失败 ");
            
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "/{courseId}/delete", method = RequestMethod.GET)
    public ModelAndView delete(@PathVariable("courseId") Long courseId) {
        ModelAndView modelAndView = new ModelAndView("market/market-course/market-course-delete");
        
        modelAndView.addObject("courseId", courseId);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param marketCourse
     * @return
     */
    @RequestMapping(value = "/{courseId}", method = RequestMethod.DELETE)
    public @ResponseBody String onDelete(@Preloading("courseId") MarketCourse marketCourse) {
        Result result = new Result();
        
        try {
            
            marketCoursewareService.deleteMarketCourse(marketCourse);
            
            result.success().message("删除课程成功");
            
        } catch (Exception e) {
            
            logger.error("onDelete - ", e);
            result.fail().message("删除课程失败 ");
            
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param courseId
     * @return
     */
    @RequestMapping(value = "/{courseId}/monthlies-edit", method = RequestMethod.GET)
    public ModelAndView editMonthly(@PathVariable("courseId") Long courseId){
        
        ModelAndView modelAndView = new ModelAndView("market/market-course/monthlies-edit");
        
        MarketCourse marketCourse = marketCoursewareService.loadMarketCourse(courseId);
        modelAndView.addObject("marketCourse", marketCourse);
        
        List<MarketMonthlyCoursewareTemplate> monthlyCoursewareTemplates = marketCoursewareService.listMonthly(marketCourse);
        modelAndView.addObject("monthlyCoursewares", monthlyCoursewareTemplates);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param courseId
     * @param monthlies
     * @return
     */
    @RequestMapping(value = "/{courseId}/monthlies-edit", method = RequestMethod.PATCH)
    public @ResponseBody String onEditMonthly(@PathVariable("courseId") Long courseId,
                                              @RequestParam("monthlies") List<String> monthlies){
        
        Result result = new Result();
        
        try{
            
            marketCoursewareService.editMonthlyCoursewareTemplates(monthlies);
            
            result.success().message("修改名称成功！");
        } catch (Exception e) {
            
            logger.error("onEditMonthly - ", e);
            result.fail().message("修改名称失败！");
        } 
        
        return JsonUtils.toJsonString(result);
    }

}
